package info

import (
	"github.com/shirou/gopsutil/net"
	"time"
)

type NetInfo struct {
	Sent    uint64 `json:"sent"`
	Recv    uint64 `json:"recv"`
	CurSent uint64 `json:"curSent"`
	CurRecv uint64 `json:"curRecv"`
}

func GetNetInfo() (interface{}, error) {
	info, err := net.IOCounters(false)
	if err != nil {
		return nil, err
	}
	netInfo := NetInfo{}

	var beforSent uint64
	var beforRecv uint64

	for _, v := range info {
		beforSent = v.BytesSent
		beforRecv = v.BytesRecv
	}

	time.Sleep(time.Second * 1)
	info, err = net.IOCounters(false)
	if err != nil {
		return nil, err
	}

	for _, v := range info {
		netInfo.Sent = v.BytesSent
		netInfo.Recv = v.BytesRecv
		netInfo.CurSent = v.BytesSent - beforSent
		netInfo.CurRecv = v.BytesRecv - beforRecv
	}

	return netInfo, nil
}
